package com.dolphin.serviceauth.service;

import com.dolphin.common.constant.CommonStatusEnum;
import com.dolphin.common.constant.IdentityConstant;
import com.dolphin.common.constant.TokenConstants;
import com.dolphin.common.dto.ResponseResult;
import com.dolphin.common.request.VerificationCodeDTO;
import com.dolphin.common.response.NumberCodeResponse;
import com.dolphin.common.response.TokenResponse;
import com.dolphin.common.util.JwtUtils;
import com.dolphin.common.util.RedisPrefixUtils;
import com.dolphin.serviceauth.remote.ServiceVerificationCodeClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class VerificationCodeService {
    @Autowired
    private ServiceVerificationCodeClient serviceVerificationCodeClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public ResponseResult generatorCode(String phone, String identity) {
        //调用验证码服务，获取验证码
        System.out.println("调用验证码服务，获取验证码服务");
        ResponseResult<NumberCodeResponse> numberCodeResponse = serviceVerificationCodeClient.getNumberCode(6);
        int numberCode = numberCodeResponse.getData().getNumberCode();
        log.info("remote 验证码：" + numberCode);

        //存入redis
        // key,value,过期时间
        String key = RedisPrefixUtils.generatorKeyByPhone(phone, identity);
        stringRedisTemplate.opsForValue().set(key, numberCode + "", 2, TimeUnit.MINUTES);

        //通过短信服务商，将对应的验证码发送到手机上。阿里短信服务，腾讯短信通，华信，容联等
        // todo

        return ResponseResult.success("");

    }


    public ResponseResult checkCode(String phone, String verificationCode, String identity) {
        //根据手机号，去redis读取验证码
        String key = RedisPrefixUtils.generatorKeyByPhone(phone, identity);
        String codeRedis = stringRedisTemplate.opsForValue().get(key);
        System.out.println("redis验证码：" + codeRedis);
        //校验验证码
        if (StringUtils.isBlank(codeRedis)) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(), CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        if (!verificationCode.trim().equals(codeRedis.trim())) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(), CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }


        //颁发令牌
        System.out.println("颁发令牌");
        String accessToken = JwtUtils.generatorToken(phone, identity, TokenConstants.ACCESS_TOKEN);
        String refreshToken = JwtUtils.generatorToken(phone, identity, TokenConstants.REFRESH_TOKEN);

        String accessTokenKey = RedisPrefixUtils.generatorTokenKey(phone, identity, TokenConstants.ACCESS_TOKEN);
        stringRedisTemplate.opsForValue().set(accessTokenKey, accessToken, 30, TimeUnit.DAYS);

        String refreshTokenKey = RedisPrefixUtils.generatorTokenKey(phone, identity, TokenConstants.REFRESH_TOKEN);
        stringRedisTemplate.opsForValue().set(refreshTokenKey, refreshToken, 31, TimeUnit.DAYS);


        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }
}

